<template>
  <div class="app-container">
    <search-box :isMulti="false">
      <template slot="content">
        <div class="left-box">
          <el-form-item label="报销类型名称">
            <el-input v-model="listQuery.typeName"
                      placeholder="请输入"
                      clearable/>
          </el-form-item>


        </div>
        <div class="right-box">
          <el-button size="mini"
                     @click="handleFilter">查询
          </el-button>
          <el-button size="mini"
                     @click="reset" type="info">重置
          </el-button>
          <el-button size="mini"
                     type="success" @click="handleCreate">新增
          </el-button>
        </div>
      </template>
    </search-box>


      <div class="table-wrap">
      <el-table :data="list"
                style="width:100%"
                element-loading-text="Loading"
                height="600">
        <el-table-column
          type="index"
          width="50"
          label="序号"
        ></el-table-column>

        <el-table-column prop="typeName"
                         label="报销类型名称">
        </el-table-column>

        <el-table-column prop="typeCode"
                         label="报销类型编码">
        </el-table-column>

        <el-table-column prop="status"
                         label="是否启用">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1">启用</span>
            <span v-else-if="scope.row.status == 2">停用</span>
          </template>
        </el-table-column>


        <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" >
              编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="total>0"
                  :total="total"
                  :page.sync="listQuery.pageNumber"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getList"/>
    </div>




    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      style="margin-top: -5%"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">

        <el-form-item label="报销类型名称" prop="typeName">
          <el-input v-model="form.typeName" ></el-input>
        </el-form-item>

        <el-form-item label="报销类型编码" prop="typeCode">
          <el-input v-model="form.typeCode" ></el-input>
        </el-form-item>

        <el-form-item label="是否启用" prop="status">
          <el-select v-model="form.status"
                     placeholder="请选择类型">
            <el-option v-for="item in reimbursementTypeList"
                       :key="item.status"
                       :label="item.status"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="onSubmit" >确 定</el-button>
  </span>
    </el-dialog>


  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import * as apiUser from '@/api/user'
  import * as meetingRoom from '@/api/meeting/meetingRoom'
  import * as reimbursementType from '@/api/finance/reimbursementType'
  import * as myProject from '@/api/projectOa/myProject'
  import * as department from '@/api/system-setting/department'
  import * as position from '@/api/system-setting/position'
  import * as user from '@/api/system-setting/user'
  import waves from '@/directive/waves' // waves directive
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  // 当然你也可以为了方便使用，将它注册到全局
  import { MessageBox } from 'element-ui'
  import bus from '@/utils/eventBus'
  import searchBox from '@/components/searchBox.vue'

  export default {
    name: 'department',
    components: { Pagination, searchBox },
    directives: { waves },
    filters: {},
    data() {
      return {
        list: null,
        total: 0,
        dialogVisible: false,
        listQuery: { // 查询列表时的参数对象
          pageNumber: 1,
          departmentName: '',
          departmentName1: '',
          departmentCode: '',
          positionName: '',
          roleName: '',
          pageSize: 50,
          name: undefined,
          proPersonLiable: undefined,
          proDepartment: undefined,
          appName: process.env.VUE_APP_APP_NAME,
          tenantIdList: []
        },
        departmentList: [
        ],


        reimbursementTypeList: [
          { 'status': '启用', 'value': 1 },
          { 'status': '停用', 'value': 2 }

        ],

        usersList: [
        ],
        listParent: [
        ],

        form: {},

        dialogTitle: '',


        rulesList: [
          {label: '管理员', value: '管理员'},
          {label: '言川一级用户', value: '言川一级用户'},
          {label: '言川二级用户', value: '言川二级用户'},
          {label: '言川三级用户', value: '言川三级用户'},

        ],

        rules: {
          typeName: [{ required: true, message: '报销类型名称必填', trigger: 'blur' }],
          typeCode: [{ required: true, message: '报销类型编码必填', trigger: 'blur' }],
          status: [{ required: true, message: '是否启用必选', trigger: 'blur' }],

        }
      }
    },
    computed: {
      ...mapGetters([
        'selectedTenantId',
        'tenantList'
      ])
    },
    created() {
      this.getList()
      // this.getRoleList()
      // this.getDepartmentList()
      // this.getDepartmentList1()

      // 监听 租户改变的事件,选择不同租户后,立马刷新数据
      bus.$on('selectedTenantIdChanged', () => {
        console.log('selectedTenantIdChanged事件触发...')
        this.getList()
      })

    },
    mounted() {
      // 测试用
      // var that = this
      // setTimeout(function() {
      //   that.dialogFormVisible = true
      // }, 5000)

      // alert(this.$store.getters.selectedTenantId);
    },
    beforeDestroy() { // 当路由切换到其他页面时触发
      bus.$off('selectedTenantIdChanged')
    },
    methods: {
      async getList() { // 获取列表数据
        this.listQuery.tenantIdList = []
        if (this.selectedTenantId == undefined) {//当前选中所有的租户
          this.tenantList.forEach((tenant, index) => {
            this.listQuery.tenantIdList.push(tenant.id)
          })
        } else {
          this.listQuery.tenantIdList.push(this.selectedTenantId)
        }

        //获取后端数据
        const { content } = await reimbursementType.getReimbursementTypeInfoList(this.listQuery)
        this.list = content.list
        this.total = content.totalCounts
      },


      async getListParent() { // 获取列表数据
        this.listQuery.tenantIdList = []
        if (this.selectedTenantId == undefined) {//当前选中所有的租户
          this.tenantList.forEach((tenant, index) => {
            this.listQuery.tenantIdList.push(tenant.id)
          })
        } else {
          this.listQuery.tenantIdList.push(this.selectedTenantId)
        }

        //获取后端数据
        const { content } = await myProject.getProjectListParent(this.listQuery)
        this.listParent = content;


      },



      async getDepartmentList() { // 获取列表数据

        const { content } = await department.getDepartmentList(this.listQuery)
        this.departmentList = content.list;

      },
      async getDepartmentList1() { // 获取列表数据

        const { content } = await department.getDepartmentList(this.listQuery)
        this.departmentList1 = content.list;

      },




      async getRoleList() { // 获取列表数据

        const { content } = await department.getRoleList(this.listQuery)
        this.rulesList = content.list;

      },
      handleFilter() {
        this.listQuery.pageNumber = 1
        this.getList()
      },

      handleCreate() {
        this.form = {}
        this.dialogVisible = true
        this.dialogTitle = '报销类型新增'
        this.listQuery = {};
        this.getListParent()
      },
      handleUpdate(row) {
        this.dialogVisible = true
        this.dialogTitle = '报销类型编辑'
        this.form = row;
        this.getListParent()

      },

      handleDelete(row) {
        this.$confirm('是否删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          reimbursementType.deleteReimbursementTypeInfo(row.id).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }).catch(() => {
        })

      },

      reset() {
        this.listQuery.typeName = ''
        this.getList()
      },
      cancel() {
        this.listQuery.typeName = ''
        this.dialogVisible = false;
        this.getList()
      },
      onSubmit(){
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.form.tenantId = this.$store.getters.selectedTenantId
            if(this.dialogTitle=='报销类型新增'){
              reimbursementType.createReimbursementTypeInfo(this.form).then((res) => {
                console.log(res);
                if(res.status==200){
                  this.$notify({
                    title: '成功',
                    message: '添加成功',
                    type: 'success',
                    duration: 2000
                  });
                  this.listQuery.typeName = ''
                  this.dialogVisible = false
                  this.getList();

                }
              })
            }else if(this.dialogTitle=='报销类型编辑'){
              reimbursementType.updateReimbursementTypeInfo(this.form).then((res) => {
                console.log(res);
                if(res.status==200){
                  this.$notify({
                    title: '成功',
                    message: '修改成功',
                    type: 'success',
                    duration: 2000
                  });
                  this.listQuery.typeName = ''
                  this.dialogVisible = false
                  this.getList();

                }
              })
            }

          } else {
            console.log('error submit!!');
            return false;
          }
        });

      }
,
      async getUsersList() { // 获取列表数据
        this.usersList = []

        const { content } = await user.getUserList(this.listQuery)
        this.usersList = content.list

      },


    }
  }
</script>

<style scoped>
  .app-select {
    margin-bottom: 20px;
  }
</style>
